package com.wcgksoft.controller.Patrol;

import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.Patrol.CompletePatrolImage;
import com.wcgksoft.entity.Patrol.PatrolAccount;
import com.wcgksoft.entity.Patrol.PatrolImage;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.ConstructionTeam.ConstructionTeamService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.Patrol.CompletePatrolImageService;
import com.wcgksoft.service.Patrol.PatrolAccountService;
import com.wcgksoft.service.Patrol.PatrolImageService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CompletePatrolController
 * @Description TODO
 * @Author 王宁
 * @Date 2020/3/21 11:43
 * @Version 1.0
 **/
@Controller
@RequestMapping("completePatrol")
public class CompletePatrolController {

    @Autowired
    private CompletePatrolImageService completePatrolImageService;

    @Autowired
    private PatrolAccountService patrolAccountService;

    @Autowired
    private PatrolImageService patrolImageService;

    @Autowired
    private GreedlandService greedlandService;

    @Autowired
    private ConstructionTeamService constructionTeamService;

    @Autowired
    private OpenAccountService openAccountService;

    /**
     * 施工角色查询巡查台账
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit,
                         @RequestParam(value = "receiverId",required = false)String receiverId){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(PatrolAccount.class);
        criteria.add(Restrictions.eq("enterpriseId", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("receiverId",receiverId));
        List<PatrolAccount> list = patrolAccountService.findByConditions(criteria,page,limit);
        for (PatrolAccount patrolAccount : list) {
            DetachedCriteria criteria1 = DetachedCriteria.forClass(PatrolImage.class);
            criteria1.add(Restrictions.eq("urisid",patrolAccount.getUrisId()));
            criteria1.add(Restrictions.eq("constructionTeamId",Integer.parseInt(patrolAccount.getReceiverId())));
            criteria1.add(Restrictions.eq("pactrolId",patrolAccount.getId()));
            List<PatrolImage> byConditions = patrolImageService.findByConditions(criteria1);
            patrolAccount.setPatrolImages(byConditions);
        }
        for (PatrolAccount patrolAccount : list) {
            DetachedCriteria criteria1 = DetachedCriteria.forClass(CompletePatrolImage.class);
            criteria1.add(Restrictions.eq("urisid",patrolAccount.getUrisId()));
            criteria1.add(Restrictions.eq("constructionTeamId",Integer.parseInt(patrolAccount.getReceiverId())));
            criteria1.add(Restrictions.eq("pactrolId",patrolAccount.getId()));
            List<CompletePatrolImage> byConditions = completePatrolImageService.findByConditions(criteria1);
            patrolAccount.setCompletePatrolImageList(byConditions);
        }
        return Layui.data(0, "查询成功", patrolAccountService.getTotalCount(), list);
    }

    /**
     * 查看绿地信息
     * @param urisId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(@RequestParam(value = "urisId",required = false)Integer urisId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        criteria.add(Restrictions.eq("id",urisId));
        criteria.add(Restrictions.eq("state",0));
        List<GreedLand> list = greedlandService.findByConditions(criteria);
        return Layui.data(0, "查询成功", greedlandService.getTotalCount(),list);
    }

    /**
     * 施工角色填完施工记录 完成时间
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public Layui save(
            HttpServletRequest request,
            @RequestParam(value = "data",required = false)String data,
            @RequestParam(value = "file",required = false) MultipartFile[] file) throws ParseException, IOException {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");

            PatrolAccount patrolAccount = (PatrolAccount) JSONObject.toBean(JSONObject.fromObject(data), PatrolAccount.class);
            String revId = (String) param.get("receiverId");
            Integer rid = Integer.parseInt(revId);
            String grId = (String) param.get("greenId");

            Integer gid = Integer.parseInt(grId);
            GreedLand byId = greedlandService.findById(gid);
            patrolAccount.setGreenName(byId.getGreedLandAdministrator().getName());
            patrolAccount.setUrisId(gid);
            SysUser byId1 = openAccountService.findById(rid);
            patrolAccount.setReceiver(byId1.getName());
            String main = (String) param.get("maintenanceTroops");
            patrolAccount.setMaintenanceTroops(main);
            patrolAccount.setProId(sysUser.getProjectid());
            //截止完成日期
            String completeTime = (String) param.get("completeTime");
            String publishTime=(String)param.get("publishTime");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            patrolAccount.setPublishTime(sdf.parse(publishTime));
            patrolAccount.setCompleteTime(sdf.parse(completeTime));
            Date complete = sdf.parse(completeTime);
            Date dateTime = new Date();

            if (dateTime.before(complete)){
                patrolAccount.setStatus("0");
            }else if (dateTime.after(complete)){
                patrolAccount.setStatus("1");
            }

            patrolAccountService.saveMerge(patrolAccount);
            for (MultipartFile mf : file) {
                CompletePatrolImage completePatrolImage = new CompletePatrolImage();
                if (!mf.isEmpty()) {
                    String filename = FileHandleUtil.Upset(mf);
                    completePatrolImage.setCompleteImage("upload/picture/" + FileHandleUtil.upload(mf.getInputStream(), "upload/picture/", filename, request));
                    completePatrolImage.setUrisid(gid);
                    completePatrolImage.setConstructionTeamId(rid);
                    completePatrolImage.setPactrolId(patrolAccount.getId());
                    completePatrolImageService.saveMerge(completePatrolImage);
                }
            }
            return Layui.data(0, "操作成功", patrolAccountService.getTotalCount(), Layui.addList(patrolAccount));
        }
    }
}